package com.stx.threeGrup.controller;

import com.stx.threeGrup.model.Results;
import com.stx.threeGrup.model.automodel.LoveBoke;
import com.stx.threeGrup.model.automodel.User;
import com.stx.threeGrup.service.LoveBokeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2019/6/13
 */
@Controller
@RequestMapping("/loveBoke")
public class LoveBokeController {
    @Autowired
    LoveBokeService loveBokeService;

    @RequestMapping("/bokeAll")
    public ModelAndView queyBokeAll(ModelAndView mav, HttpSession session) {
        List<LoveBoke> loveBokes = loveBokeService.queryNewsAll();
        session.setAttribute("bokeList", loveBokes);
        mav.addObject("bokeList", loveBokes);
        mav.setViewName("WEB-INF/houtai/newslist");
//        request.getRequestDispatcher()
        return mav;
    }

    @RequestMapping("/delBokeById")
    public ModelAndView delBokeById(Integer bokeId, ModelAndView mav, HttpServletRequest request, HttpSession session) {
        boolean flag = loveBokeService.delNewById(bokeId);
        ModelAndView modelAndView = null;
        System.out.println(flag);
        if (flag) {
            request.setAttribute("message", "删除成功");
            modelAndView = queyBokeAll(mav, session);
        }
//        mav.setViewName("WEB-INF/houtai/newslist");
        return modelAndView;
    }

    @RequestMapping("/bokeAllById")
    public @ResponseBody
    Results queyBokeAllById(Integer userId) {
//        User user = (User) userSession.getAttribute("user");
        System.out.println(userId);
        System.out.println(1);
        List<LoveBoke> loveBokes = loveBokeService.queryNewAllById(userId);
        if (loveBokes != null) {
            return new Results("获取成功", 1).addObject("bokes", loveBokes);
        }
        return new Results("获取失败", 0);
    }

    @RequestMapping("/delNewByUserIdAndNesId")
    public @ResponseBody
    Results delNewByUserIdAndNesId(LoveBoke boke) {
        System.out.println(boke.getUserId());
        System.out.println(boke.getBokeId());
        boolean flag = loveBokeService.delNewByUserIdAndNesId(boke);
        if (flag) {
            return new Results("删除成功", 1);
        }
        return new Results("删除失败", 0);
    }

    @RequestMapping("/addNewByUserId")
    public @ResponseBody
    Results addNewByUserId(LoveBoke boke) {
        System.out.println(boke.getBokeContext());
        boolean flag = loveBokeService.addNewByUserId(boke);
        if (flag) {
            return new Results("增加成功", 1);
        }
        return new Results("增加失败", 0);
    }

}
